*************************************************
********INDIVIDUAL Level Analysis****************
*************************************************

* directory definitions 

	project, doinfo
	global pdir "`r(pdir)'"							// the project's main dir.
	global dofile "`r(dofile)'"						// do-file's stub name
	global data_original = "$pdir/data_original"  //data directory for coded data 
	global data_coded = "$pdir/data_coded"  //data directory for coded data 
	global figures = "$pdir/results/figures"  //data directory for figures
	global tables = "$pdir/results/tables"  //data directory for tables

* 
	// report any data we create previously and we do need here: 
	project, uses("$data_coded/placebased_individual.dta")

* open individual level panel data:
	
	use "$data_coded/placebased_individual.dta", clear 
	xtset 





***MAINBODY***

* estimation: 

	// define locals: 
	local control_soc eco_general unemployed_3 female_3 age_3 edu_3 religious_3 
	local control_pol m5s_1 pd_1 eco_general_1 lr_no_1 polinterest_1 talk_politics_1 pol_complex_1 internal_efficacy_1
	// OLS estimates (tab 2): 
	eststo clear
	eststo: reghdfe referendum_no_wunsure m5s_wn_wave_157, absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_wn_wave_157 `control_soc', absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_wn_wave_157 `control_soc' `control_pol', absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_wn_wave_157, absorb(id post) cluster(id comune_id)
	ebalance m5s_ever `control_soc' `control_pol'
	eststo: reghdfe referendum_no_wunsure m5s_wn_wave_157 [aweight=_webal], absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_wn_wave_157 [aweight=_webal], absorb(id post) cluster(id comune_id)
	** create a LaTeX table:
	estfe est*, labels(id "Individual FE" post "Wave FE" comune_id "Municipality FE")
	** save the table:
	esttab est* using "$tables/tab2_ols_individual.tex", replace  ///
	indicate( `r(indicate_fe)' "Socio-economic controls=unemployed_3" "Political controls=pd_1", labels(\checkmark)) ///
	se nobaselevels nostar label se(2) b(2) ///
	drop(eco_general female_3 age_3 edu_3 religious_3 polinterest_1 talk_politics_1 eco_general_1 lr_no_1 pol_complex_1 internal_efficacy_1) ///
	stats(N N_clust2 r2_a r2_a_within rmse, fmt(%9.0f %9.0f %9.2f %9.2f %9.2f)  labels("Obs" "Municipalities" "adj.R\$^2$" "adj.R\$^2$ (within)" "RMSE")) ///
	note("\emph{Note:} Clustered standard errors by individual$\times \$ municipality in parentheses. Controls omitted from table: economy retrospective (1-5), unemployed (0,1), female (0,1), age (18-88), education (1-7), religiosity (0,1), PD voter in 2013 (0,1), political interest (1-4), talk politics (1-6), explicitly no left-right self-placement (0,1), politics too complex (1-4), internal efficacy (1-4). For entropy balancing we use only variables asked in the 2013 post election study as outlined in Figure \ref{fig:iv_balance}.") ///
	substitute(_ _) 

	// one town simulation: 100 inhabitants, 0 vs 1 event, 10% rsvp
	reghdfe referendum_no_wunsure m5s_wn_wave_157, absorb(id post) cluster(id comune_id)
	margins, at(m5s_wn_wave_157=(0 0.40546511))
	dis .4233418 -  .3943116 
	** .0290302

	// "manipulation" test (fig 6b)
	** estimate models for M5S:
	cls
	eststo clear
	foreach var of varlist web_politican_4-web_event_4 {
	gen `var'_web=m5s_ref_yn 
	eststo: reghdfe `var' `var'_web `control_soc' `control_pol' [aweight=_webal], absorb(province_1) cluster(comune_id)
	drop `var'_web
	}
	**estimate models for adjacent: 
	foreach var of varlist web_politican_4-web_event_4 {
	gen `var'_web=m5s_ref_neig_yn 
	eststo: reghdfe `var' `var'_web `control_soc' `control_pol' [aweight=_webal] if m5s_ref_yn==0, absorb(province_1) cluster(comune_id)
	drop `var'_web
	}
	** coefplot: 
	coefplot ///
	(est6, offset(0) ciopts(lwidth(0.6 ..)) msymbol(d) msize(medlarge) mlwidth(medthick)) ///
	(est1, offset(0) pstyle(p3)) ///
	(est2, offset(0) pstyle(p3)) ///
	(est3, offset(0) pstyle(p3)) ///
	(est4, offset(0) pstyle(p3)) ///
	(est5, offset(0) pstyle(p3)) ///
	, legend(off) bylabel("{it:M5S active locally}") xtitle("{it:from less to more frequently used} (1-4)") ///
	|| ///
	(est12, offset(0)) ///
	(est7, offset(0)) ///
	(est8, offset(0)) ///
	(est9, offset(0)) ///
	(est10, offset(0)) ///
	(est11, offset(0)) ///
	, ///
	keep(*_contact *_web) level(95) title("{bf:B) information-seeking}") legend(off) xline(0) ///
	bylabel("{it: M5S active in neighboring municipality}") byopts(legend(off))  ///
	xtitle("{it:from less to more frequently used} (1-4)") ///
	ciopts(lwidth(0.4 ..)) mlwidth(medthick) msize(medsmall) mfcolor(white) ///
	groups(*_contact = "{bf: information}" *_web = "{bf: web}", angle(rvertical)) ///
	grid(none) ///
	yscale(alt axis(1)) ///
	ylabel( ///
	1 "{bf:offline event organized via web}" /// 
	2 "social media profile of party" ///
	3 "videos on campaigns" ///
	4 "shared contents about campaign" /// 
	5 "debated campaign online" /// 
	6 "website of politician" ///
	) 
	** save as pdf: 
	graph export "$figures/fig6b_manipulation.pdf", replace
	
	// in-partisan effect (fig 7b):
	** estimate: 
	reghdfe referendum_no_wunsure c.m5s_wn_wave_157##i.m5s_1 eco_general unemployed_3 female_3 age_3 edu_3 religious_3 pd_1 pdl_1 lr_no_1 polinterest_1 talk_politics_1 eco_general_1 pol_complex_1 internal_efficacy_1, absorb(comune_id post) cluster(id comune_id)
	** calculate margins: 
	margins, dydx(m5s_1) at(m5s_wn_wave_157=(0(.1)5))
	** produce figure:
	marginsplot, ///
	yline(0) ///
	xtitle("M5S activity (cont.)") ///
	ytitle("Marginal effects on {bf:Referendum: No}") ///
	title("{bf: B) in-partisan}") ///
	recast(scatter) recastci(rspike) ///
	ciopts(col(uzhblue%50)) ///
	plotopts(msymbol(O) mlcolor(uzhblue) mfcolor(white))
	** save as pdf: 
	graph export "$figures/fig7b_partisan.pdf", replace
	




***SUPPORTING INFORMATION***
	
	use "$data_coded/placebased_individual.dta", clear 
	xtset 

	// define locals: 
	local control_soc eco_general unemployed_3 female_3 age_3 edu_3 religious_3 
	local control_pol m5s_1 pd_1 eco_general_1 lr_no_1 polinterest_1 talk_politics_1 pol_complex_1 internal_efficacy_1

	// report balance figure (fig a9)
	** standardize:
	standard2 polinterest_1 talk_politics_1 female_1 age_1 edu_1 unemployed_1 pol_complex_1 internal_efficacy_1 eco_general_1
	** estimate:
	ebalance m5s_ever `control_soc' `control_pol'
	eststo clear 
	foreach var of varlist female_1 std_age_1 std_edu_1 unemployed_1 m5s_1 pd_1 lr_no_1 std_polinterest_1 std_talk_politics_1 std_pol_complex_1 std_internal_efficacy_1 std_eco_general_1 {
		rename  m5s_ever `var'_treat
		eststo raw_`var': reg `var' `var'_treat if wave==1
		rename  `var'_treat m5s_ever 
	}
	foreach var of varlist female_1 std_age_1 std_edu_1 unemployed_1 m5s_1 pd_1 lr_no_1 std_polinterest_1 std_talk_politics_1 std_pol_complex_1 std_internal_efficacy_1 std_eco_general_1 { 
		rename  m5s_ever `var'_treat
		eststo wgt_`var': reg `var' `var'_treat [aweight=_webal] if wave==1
		rename  `var'_treat m5s_ever 
	}
	** coefplot: 
	coefplot ///
	(raw_*) ///
	(wgt_*, msymbol(D)) ///
	, keep(*treat) xline(0) ///
	ciopts(lwidth(0.4 ..)) mlwidth(medthick) msize(medsmall) mfcolor(white) ///
	legend(order(2 "raw" 4 "entropy balanced")) ///
	xtitle("standardized {it:differences} in means") ///
	groups(female_1_treat unemployed_1_treat = "{bf:socio-economic}" m5s_1_treat std_internal_efficacy_1_treat = "{bf:politics & voting}", angle(rvertical)) ///
	grid(none) ///
	ylabel( ///
	1 "female {it:(0,1)}" ///
	2 "age {it:(19-85)}" ///
	3 "education {it:(1-4)}" ///
	4 "unemployed {it:(0,1)}" ///
	6 "M5S voter 2013 {it:(0,1)}" ///
	7 "PD voter 2013 {it:(0,1)}" ///
	8 "explicitly no lr placement {it:(0,1)}" ///
	9 "political interest {it:(1-4)}" ///
	10 "talk politics {it:(1-6)}" ///
	11 "politics too complex {it:(1-4)}" ///
	12 "internal efficacy {it:(1-4)}" ///
	14 "economy retrospective {it:(1-5)}" ///
	) 
	*** save as pdf: 
	graph export "$figures/figa9_balance.pdf", replace
	
	// events only (tab a7): 
	** estimate all models: 
	eststo clear
	eststo: reghdfe referendum_no_wunsure m5s_n_wave_157, absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_n_wave_157 `control_soc', absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_n_wave_157 `control_soc' `control_pol', absorb(comune_id post) cluster(id comune_id)
	eststo: reghdfe referendum_no_wunsure m5s_n_wave_157, absorb(id post) cluster(id comune_id)
	ebalance m5s_ever `control_soc' `control_pol'
	eststo: reghdfe referendum_no_wunsure m5s_n_wave_157 [aweight=_webal], absorb(comune_id post) cluster(id comune_id)
	** create a LaTeX Table:
	estfe est*, labels(id "Individual FE" post "Wave FE" comune_id "Municipality FE")
	** save table:
	esttab est* using "$tables/taba7_events.tex", replace  ///
	indicate( `r(indicate_fe)' "Socio-economic controls=unemployed_3" "Political controls=pd_1", labels(\checkmark)) ///
	se nobaselevels nostar label se(2) b(2) ///
	drop(eco_general female_3 age_3 edu_3 religious_3 polinterest_1 talk_politics_1 eco_general_1 lr_no_1 pol_complex_1 internal_efficacy_1) ///
	stats(N N_clust2 r2_a r2_a_within rmse, labels("Obs" "Municipalities" "adj.R\$^2$" "adj.R\$^2$ (within)" "RMSE")) ///
	note("\emph{Note:} Clustered standard errors by individual$\times \$ municipality in parentheses. Controls omitted from table: economy retrospective (1-5), unemployed (0,1), female (0,1), age (18-88), education (1-7), religiosity (0,1), PD voter in 2013 (0,1), political interest (1-4), talk politics (1-6), explicitly no left-right self-placement (0,1), politics too complex (1-4), internal efficacy (1-4). For entropy balancing we use only variables asked in the 2013 post election study as outlined in Figure \ref{fig:iv_balance}.") ///
	substitute(_ _) 
	
	// heterogenoues effects (tab a10): 
	** estimate:
	eststo clear
	foreach dv of varlist referendum_no_wunsure turnout { 
		eststo: reghdfe `dv' c.m5s_wn_wave_157##i.m5s_1 `control_soc' pd_1 pdl_1 lr_no_1 polinterest_1 talk_politics_1 eco_general_1 pol_complex_1 internal_efficacy_1, absorb(comune_id post) cluster(id comune_id)
		eststo: reghdfe `dv' c.m5s_wn_wave_157##i.pd_1 m5s_1 pdl_1 `control_soc' lr_no_1 polinterest_1 talk_politics_1 eco_general_1 pol_complex_1 internal_efficacy_1, absorb(comune_id post) cluster(id comune_id)
		eststo: reghdfe `dv' c.m5s_wn_wave_157##i.pdl_1 pd_1 m5s_1 `control_soc' lr_no_1 polinterest_1 talk_politics_1 eco_general_1 pol_complex_1 internal_efficacy_1, absorb(comune_id post) cluster(id comune_id)
		eststo: reghdfe `dv' c.m5s_wn_wave_157##i.referendum_unsure_3 pdl_1 pd_1 m5s_1 `control_soc' lr_no_1 polinterest_1 talk_politics_1 eco_general_1 pol_complex_1 internal_efficacy_1, absorb(comune_id post) cluster(id comune_id)
	}
	** create a LaTeX Table:
	estfe est*, labels(post "Wave FE" comune_id "Municipality FE")
	** save table: 
	esttab est* using "$tables/taba10_hte.tex", replace  ///
	indicate( `r(indicate_fe)' "Socio-economic controls=unemployed_3" "Political controls=pd_1", labels(\checkmark)) ///
	se nobaselevels nostar label se(2) b(2) ///
	drop(eco_general female_3 age_3 edu_3 religious_3  pdl_1  lr_no_1 polinterest_1 talk_politics_1 eco_general_1  pol_complex_1 internal_efficacy_1) ///
	stats(N N_clust2 r2_a r2_a_within rmse,  fmt(%9.0f %9.0f %9.2f %9.2f %9.2f) labels("Obs" "Municipalities" "adj.R\$^2$" "adj.R\$^2$ (within)" "RMSE")) ///
	note("\emph{Note:} Clustered standard errors by individual$\times \$ municipality in parentheses. Controls omitted from table: economy retrospective (1-5), unemployed (0,1), female (0,1), age (18-88), education (1-7), religiosity (0,1), PD voter in 2013 (0,1), political interest (1-4), talk politics (1-6), explicitly no left-right self-placement (0,1), politics too complex (1-4), internal efficacy (1-4). For entropy balancing we use only variables asked in the 2013 post election study as outlined in Figure \ref{fig:iv_balance}.") ///
	substitute(_ _) ///
	mgroups("Vote: No" "Turnout", pattern( 0 0 0 0 1 0 0 0 1) ///
	prefix(\multicolumn{@span}{c}{) suffix(}) ///
	span erepeat(\cmidrule(lr){@span})) 

	// remaining ref outcomes (fig a11)
	** estimate all models: 
	cls
	eststo clear
	foreach var of varlist referendum_yes_wunsure turnout {
		eststo: reghdfe `var' m5s_wn_wave_157, absorb(comune_id post) cluster(id comune_id)
		eststo: reghdfe `var' m5s_wn_wave_157 `control_soc', absorb(comune_id post) cluster(id comune_id)
		eststo: reghdfe `var' m5s_wn_wave_157 `control_soc' `control_pol', absorb(comune_id post) cluster(id comune_id)
		eststo: reghdfe `var' m5s_wn_wave_157, absorb(id post) cluster(id comune_id)
		ebalance m5s_ever `control_soc' `control_pol'
		eststo: reghdfe `var' m5s_wn_wave_157 [aweight=_webal], absorb(comune_id post) cluster(id comune_id)
	}
	** plot all coefficients: 
	coefplot ///
	est1 est2 est3 est4 est5, legend(off) bylabel("{bf:Referendum: 'Yes'}")  ///
	|| ///
	est6 est7 est8 est9 est10,    /// 
	drop(_cons) ///
	ciopts(lwidth(0.4 ..)) mlwidth(medthick) msize(medsmall) mfcolor(white) ///
	byopts(legend(off)) ///
	bylabel("{bf:Referendum: turnout}") ///
	xline(0)  ///
	grid(none) 
	** save as pdf: 
	graph export "$figures/figa11_otheroutcomes.pdf", replace
	
	// summary statistics (a13)
	** clear from previous results: 
	est clear  
	** summarize:
	estpost tabstat referendum_no_wunsure m5s_wn_wave_157 ///
	eco_general unemployed_3 female_3 age_3 edu_3 religious_3 m5s_1 pd_1 eco_general_1 lr_no_1 polinterest_1 talk_politics_1 eco_general_1 lr_no_1 pol_complex_1 internal_efficacy_1 ///
	if (wave == 3 | wave == 4) & referendum_no_wunsure !=., ///
	c(stat) stat(mean sd min max n)
	** save: 
	esttab using "$tables/taba13_summary_individual.tex", replace ///
 	cells("mean(fmt(%13.2fc)) sd(fmt(%13.2fc)) min max count") nonumber ///
  	nomtitle nonote noobs label collabels("Mean" "SD" "Min" "Max" "N")
